package user_controller

import (
	"gorm-demo/database"
	"gorm-demo/entity"
	"gorm-demo/util"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func AddUser(c *gin.Context) {
	var user entity.User
	err := c.ShouldBindJSON(&user)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	id := uuid.New().String()
	createTime := util.GetCurrentTime()
	updateTime := createTime
	user.Id = id
	user.CreateTime = createTime
	user.UpdateTime = updateTime
	result := database.DB.Create(user)
	if result.Error != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": result.Error.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "添加成功",
	})
}

func GetUserList(c *gin.Context) {
	var userList []entity.User
	database.DB.Find(&userList)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查询成功",
		"data":    userList,
	})
}

func GetUserOlderThan(c *gin.Context) {
	var user entity.User
	err := c.ShouldBindJSON(&user)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var userList []entity.User
	database.DB.Where("age > ?", user.Age).Find(&userList)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查询成功",
		"data":    userList,
	})
}

func GetUserLike(c *gin.Context) {
	var user entity.User
	err := c.ShouldBindJSON(&user)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var userList []entity.User
	database.DB.Where("username like ?", "%"+user.Username+"%").Find(&userList)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查询成功",
		"data":    userList,
	})
}

func UpdateUserById(c *gin.Context) {
	var user entity.User
	err := c.ShouldBindJSON(&user)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    "500",
			"message": "输入有误",
			"error":   err.Error(),
		})
		return
	}
	var user1 entity.User // 获取数据库中的相应记录
	// 根据id获取user1
	user1Result := database.DB.Where("id = ?", user.Id).First(&user1)
	if user1Result.Error != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    "500",
			"message": "未从数据库中查询到数据",
			"error":   user1Result.Error.Error(),
		})
		return
	}
	createTime := user1.CreateTime
	updateTime := util.GetCurrentTime()
	user.CreateTime = createTime
	user.UpdateTime = updateTime
	result := database.DB.Save(&user)
	if result.Error != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    "500",
			"message": "更新失败",
			"error":   result.Error.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    "200",
		"message": "更新成功",
	})
}

func UpdateUsernameById(c *gin.Context) {
	var params entity.User
	err := c.ShouldBindJSON(&params)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    "400",
			"message": "参数错误",
			"error":   err.Error(),
		})
		return
	}
	id := params.Id
	username := params.Username
	result := database.DB.Model(&entity.User{}).Where("id = ?", id).Update("username", username)
	if result.Error != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    "400",
			"message": "更新失败",
			"error":   result.Error.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    "200",
		"message": "更新成功",
	})
}

func DeleteUserById(c *gin.Context) {
	var params entity.User
	err := c.ShouldBindJSON(&params)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    "400",
			"message": "参数错误",
			"error":   err.Error(),
		})
		return
	}
	// 根据id删除数据
	id := params.Id
	err = database.DB.Where("id = ?", id).Delete(&entity.User{}).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    "500",
			"message": "删除失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    "200",
		"message": "删除成功",
	})
}

func GetUserListBySql(c *gin.Context) {
	var userList []entity.User
	err := database.DB.Raw("select * from user").Scan(&userList).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    "500",
			"message": "查询失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    "200",
		"message": "查询成功",
		"data":    userList,
	})
}

func UpdateUserAgeBySql(c *gin.Context) {
	result := database.DB.Exec("update user set age = age + 1")
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    "500",
			"message": "更新失败",
			"error":   result.Error.Error(),
		})
		return
	}
	// 获取受影响的行
	rowCount := result.RowsAffected
	c.JSON(http.StatusOK, gin.H{
		"code":    "200",
		"message": "更新成功",
		"data":    rowCount,
	})
}
